let app = getApp();
let iconUrl = 'https://atush.vip/static/icons/';

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        userInfo: null,
        showSignOut: false,
        iconUrl: iconUrl,
        headerList: [
            { id: 1, name: 'contact', iconUrl: '/icon/kefu.svg' },
            { id: 2, name: 'release', iconUrl: '/icon/send.svg' },
            { id: 3, name: 'favorites', iconUrl: '/icon/shoucang.svg' }
        ],
        mainLists: [
            { id: 1, name: 'notice', prompt: '', iconUrl: iconUrl +'notice.svg' },
            // { id: 2, name: 'authenticate', prompt: 'pass', iconUrl: iconUrl +'renzheng.svg' },
            { id: 2, name: 'setting', prompt: 'lang', iconUrl: iconUrl + 'shezhi.svg' },
            { id: 3, name: 'complaint', prompt: '', iconUrl: iconUrl +'tousu.svg' },
            { id: 4, name: 'advertising', prompt: '', iconUrl: iconUrl +'guanggao.svg' },
            { id: 5, name: 'aboutMe', prompt: 'version', iconUrl: iconUrl +'guanyu.svg' },
            { id: 6, name: 'heaDoffice', prompt: 'heaDofficeTEL', iconUrl: iconUrl +'telephone.svg' }
        ],
        lang: null,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        handleContact(e) {
            console.log(e);
        },

        // 登录
        loginBtn() {
            app.login(res => {
                this.setData({ userInfo: res.user })
            })
        },

        // 检查用户登录
        isLogin() {
            let userInfo = wx.getStorageSync('userInfo')
            if (!userInfo) {
                app.login( res => {
                    this.setData({ userInfo: res.user })
                })
            } else {
                this.setData({ userInfo: userInfo.user })
            }
        },

        /**
         * 购买会员
         */
        buyAuth() {
            wx.navigateTo({ url: '/pages/detail/buyAuth/buyAuth' });
        },

        /**
         * headerList 跳页
         */
        headerList(event) {
            let index = event.currentTarget.dataset.index
            let checkAuth = false;
            switch (index) {
                case 1:
                    let promise = new Promise(function(resolve, rej) {
                        app.login(res => {
                            resolve(res)
                        })
                    })

                    checkAuth = true
                    promise.then(
                        resolve => {
                            wx.navigateTo({ url: '/pages/mine/messages/messages' });
                        }
                    )
                    break;
                case 2:
                    let promise1 = new Promise(function(resolve, rej) {
                        app.login(res => {
                            resolve(res)
                        })
                    })

                    checkAuth = true
                    promise1.then(
                        resolve => {
                            wx.navigateTo({ url: '/pages/mine/favorites/favorites' });
                        }
                    )
                    break;
                default:
                    break;
            }
        },

        /**
         * mainList跳页
         */
        mainList(event) {
            let index = event.currentTarget.dataset.index
            let item = event.currentTarget.dataset.item
            switch (index) {
                case 0:
                    wx.navigateTo({
                        url: '/pages/mine/notice/notice'
                    });
                    break;
                // case 1:
                //     wx.navigateTo({
                //         url: '/pages/detail/authenticate/authenticate'
                //     });
                //     break;
                case 1:
                    wx.navigateTo({
                        url: '/pages/mine/complaint/complaint'
                    });
                    break;
                case 2:
                    wx.navigateTo({
                        url: '/pages/mine/setUp/setUp'
                    });
                    break;
                case 3:
                    wx.navigateTo({
                        url: '/pages/mine/advertising/advertising'
                    });
                    break;
                case 4:
                    wx.navigateTo({
                        url: '/pages/mine/about/about'
                    });
                    break;
                default:
                    console.log('参数错误！！！');
                    break;
            }
        },

        // 联系总部
        onContact() {
            wx.makePhoneCall({
                phoneNumber: '15700997760',
                fail: cancel => {
                    console.log("cancel: ", cancel);
                }
            })
        },

        // 退出登录
        signOut() {
            this.setData({ showSignOut: true });
        },

        // 确认退出
        signOutOnConfirm() {
            wx.setStorage({
                key: "userInfo",
                data: '',
            })
            this.setData({ showSignOut: false });
            wx.reLaunch({ url: "/pages/index/index" });
        },

        // 取消退出
        signOutOnCancel() {
            this.setData({ showSignOut: false });
        }
    },
    lifetimes: {
        created() {},

        ready() {},

        attached: function () {
            this.setData({ lang: app.globalData.lang })
            this.isLogin()
        }
    }
})
